javascript - Jquery 无法创建新的输入

标签 javascript jquery html

下面是我的 Html 文档,当范围输入更改时,JQuery 不会执行任何操作,非常感谢任何帮助,因为我对网页设计非常陌生。即使警报在顶部也不起作用,所以我不确定我的问题是什么。我的信念是脚本永远不会被调用,或者它是一个 html 文档有问题,但无论如何谢谢你。

     <!doctype html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
        var num=0;
        var numOptions = new Array(100);
        window.onload = function() {
            if (window.jQuery) {  
                // jQuery is loaded  
                alert("Yeah!");
            } else {
                // jQuery is not loaded
                alert("Doesn't Work");
            }
        }
        $(document).ready(function(){
            $("#numQuestions").on('input',function(){
            var numbQuestions = $("#numQuestions".text());
            if(num>numbQuestions){
                for(i=numbQuestions;i<=num;i++){
                    try{
                        $("#qRowNum'+i).remove();
                        }catch(err){

                        }
                    }
                    }else{
                    for ( i=num;  i < numbQuestions;  i++) 
                    { 
                        var row = '<div id="qRowNum'+ i '">   

#the below function is not implemented in this version 
<input type="text" placeholder="Question '+i'">    <input type="range" name="numOptions'+i'" min="0" max="5" placeholder="Number Of Options" onchange="CreateOptions(this);" onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();> </div>';
                        $("#questionRows").append(row);
                        //New script test


                    }
                }
                num = numbQuestions;
            });
        });
                           <div id="questionRows">

            </div>
            <input type="submit" value="Start">
        </form>

            </body>
        </html> 

最佳答案

这是你的问题:

var numbQuestions = $("#numQuestions".text());

首先我认为你的意思是:

var numbQuestions = $("#numQuestions").text();

但这也不是真的,因为 input 字段没有 text 属性。它们有值(value)所以这样做:

var numbQuestions = $("#numQuestions").val();

这是另一个问题: $("#qRowNum'+i) 当你用双引号开始选择器时,你也需要用双引号结束它。但这仍然不是一个真正的选择器jquery 选择器。

我认为你需要更多地研究jquery。

关于javascript - Jquery 无法创建新的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958446/

相关文章:

javascript - TinyMCE 不在控制台中输出文本

javascript - 在浏览器关闭时向服务器发送数据

javascript - 单击时使用 ajax 调用重新加载数据表数据

jquery - 用 div 填充容器

html - 引用标签不会在 Chrome 中使文本变为斜体

javascript - 当填写超过 2 个字符时添加第二个输入。 (javascript)(很难)

javascript - react : How to pass html as prop

javascript - 在 Vue.js 中有条件地定位具有 CSS 样式的类

javascript - 使用 jQuery 获取所有输入值的总和

javascript - 还有另一个 JSON -> HTML 表线程