Javascript JS在一个地方工作,但在另一个地方不工作

Javascript JS在一个地方工作,但在另一个地方不工作,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我被一件有点奇怪的事情难住了。我在一个html文件中修改了一些代码,上面有js和html,直到我让它按照我想要的方式工作,然后我复制并粘贴到链接到js页面的php文件中。问题是,在php页面中,我收到错误消息unrecognized expression:select[name=gatehight]option:selected。如果有人能解释我搞砸了什么,我会非常感激的! 以下是包含JS和html的html文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HT

我被一件有点奇怪的事情难住了。我在一个html文件中修改了一些代码,上面有js和html,直到我让它按照我想要的方式工作,然后我复制并粘贴到链接到js页面的php文件中。问题是,在php页面中,我收到错误消息unrecognized expression:select[name=gatehight]option:selected。如果有人能解释我搞砸了什么,我会非常感激的! 以下是包含JS和html的html文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

                // create the new element via clone(), and manipulate it's ID using newNum value
                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

                // manipulate the name/id values of the input inside the new element
                newElem.children($("select[name=gateHeight] option: selected")).attr('id', 'gateHeight' + newNum).attr('name', 'gateHeight' + newNum);
                //newElem.children('input[type=text]:first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
                newElem.children('input[type=checkbox]:first').attr('id', 'chk' + newNum).attr('name', 'chk' + newNum);

                // insert the new element after the last "duplicatable" input field
                $('#input' + num).after(newElem);

                // enable the "remove" button
                //$('#btnDel').attr('disabled','');
                $('#btnDel').removeAttr('disabled');

                // business rule: you can only add 5 names
                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

            $('#btnDel').click(function() {
                var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                $('#input' + num).remove();     // remove the last element

                // enable the "add" button
                //$('#btnAdd').attr('disabled','');
                $('#btnAdd').removeAttr('disabled');

                // if only one element remains, disable the "remove" button
                if (num-1 == 1)
                    $('#btnDel').attr('disabled','disabled');
            });

            $('#btnDel').attr('disabled','disabled');
        });
    </script>
</head>

<body>

<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
       <!-- <input type="text" name="name1" id="name1" /> -->
        <select name="gateHeight" id="gateHeight">
                            <option value="select">Select Gate Height</option>
                            <option value="4fg">4 Ft. Gate</option>
                            <option value="6fg">6 Ft. Gate</option>
                            <option value="8fg">8 Ft. Gate</option>
        </select> 
        Include Spring Pool Latch: <input type="checkbox" name="chk1" id="chk1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
        <input type="button" id="btnDel" value="remove name" />
    </div>
</form>
</body>
HTML:


选择浇口高度
4英尺大门
6英尺大门
8英尺大门
包括弹簧池闩锁:

您需要在
选项:selected
中去掉冒号后面的空格。只需将其更改为
option:selected
您需要在
option:selected
中去掉冒号后面的空格。只需将其更改为
选项:选中

尝试

$("select[name=gateHeight] option :selected")
:selected
是正确的,而不是
:selected
(无空格)

这将起作用

尝试使用

$("select[name=gateHeight] option :selected")
:selected
是正确的,而不是
:selected
(无空格)


这将起作用。

您缺少文档准备包装函数,因此在元素存在之前调用了代码。

您缺少文档准备包装函数,因此在元素存在之前调用了代码,因此失败。

首先您应该使用
jquery
类似于jquery-1.9或更高版本

Second您应该使用
newElem.children(“选择[name=gatehight]”)来代替
newElem.children($(“选择[name=gatehight]选项:选中”)


首先您应该使用
jquery
更新版本,如jquery-1.9或更高版本

Second您应该使用
newElem.children(“选择[name=gatehight]”)来代替
newElem.children($(“选择[name=gatehight]选项:选中”)


你们都是些怪诞的天才。哈哈。我仍然不明白为什么它在一个文件中工作,但在另一个文件中却不工作,但是哦,好吧!你们都是些古怪的天才。哈哈。我仍然不明白为什么它在一个文件中工作,但在另一个文件中却不工作,但是哦,好吧!你们都是些古怪的天才。哈哈。我仍然不明白为什么它在一个文件中工作,但在另一个文件中却不工作,但是哦,好吧!你们都是些古怪的天才。哈哈。我仍然不明白为什么它在一个文件中工作,但在另一个文件中却不工作,但是哦,好吧!
$("select[name=gateHeight] option :selected")
$("select[name=gateHeight] option").filter(":selected")
newElem.find("select[name='gateHeight']")
           .attr({'id': 'gateHeight'+newNum, 'name': 'gateHeight' + newNum});// combine attr() using json