繁体   English   中英

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

[英]JS works in one place but not another

我感到有些奇怪。 我正在修改其中包含js和html的html文件中的一些代码,直到我可以按需要运行它,然后将其复制并粘贴到链接到JS页面的php文件中。 麻烦的是,在php页面中,我得到了错误消息无法识别的表达式:select [name = gateHeight] 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>

这是另一个文件的JS和HTML:JS:

//Dynamic Gate Input Fields
    $('#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');

HTML:

<div id="input1" style="margin-bottom:4px;" class="clonedInput">
                        <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 Gate" />
                <input type="button" id="btnDel" value="Remove Gate" />
            </div>

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

您需要去除option: selected的冒号后面的空格option: selected 只需将其更改为option:selected

试试看

$("select[name=gateHeight] option :selected")

:selected是正确的一个not : selected (没有空格)

要么

$("select[name=gateHeight] option").filter(":selected")

这会起作用

首先,您应该使用jqueryupdated version ,例如jquery-1.9或更高版本。

其次,您应该使用newElem.children("select[name=gateHeight]")代替newElem.children($("select[name=gateHeight] option: selected"))

newElem.find("select[name='gateHeight']")
           .attr({'id': 'gateHeight'+newNum, 'name': 'gateHeight' + newNum});// combine attr() using json

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM