简体   繁体   English

jQuery输入值不起作用

[英]jQuery input value not working

Have looked for a solution to this but not found one. 寻找了一个解决方案,但没有找到一个。

$(document).ready(function() {
    if ($("input").value()) {
        $("h1").hide();
    }
}

So this does not seem to be working ( $("h1").hide() is just a placeholder action... that part is not important, the problem is that the if statement is not working). 因此,这似乎不起作用( $("h1").hide()只是一个占位符操作……该部分并不重要,问题在于if语句不起作用)。

There is one form on the page, <input type=text> . 页面上有一种形式, <input type=text> I want to make it so that at all times, if there is any text in the input box a certain state is applied. 我要确保在任何时候,如果输入框中有任何文本,都会应用某种状态。 If the input box returns to empty then the state is removed. 如果输入框返回空,则状态将被删除。

There are quite a few other functions inside the $(document).ready function which I omitted due to clarity... but as far as the scope of where the if statement lies it is directly inside of the document.ready function. $(document).ready函数中还有很多其他函数,出于清楚的原因我省略了它们……但是就if语句所在的范围而言,它直接位于document.ready函数内部。 PS The form is shown and hidden dynamically, but it is hard coded -- it is not being created dynamically. PS表单是动态显示和隐藏的,但是它是硬编码的-它不是动态创建的。

What is wrong with where I have this if statement located? 如果将if语句放在哪里,怎么了?

Try with .val() like 尝试使用.val()

$(document).ready(function(){
    if ($("input").val()) {
        $("h1").hide();
    }
});

Better you use either name or id or class names as selectors .Because input can be of any number and they also includes checkboxes , radio buttons and button types 最好使用nameidclass names作为选择器 。因为input可以是任何数字,并且它们还包括checkboxesradio buttonsbutton类型

In jQuery you have to use .val() method and not .value() . 在jQuery中,您必须使用.val()方法,而不是.value() Your check should be as follows: 您的支票应如下所示:

if ($("input").val()) {
 $("h1").hide();
}

.value() is invalid. .value()无效。 You should use .val() instead of .value() . 您应该使用.val()而不是.value() Hope it will make it work? 希望它能起作用?

Unlike .value() in JS , ther's .val() in JQuery . JS .value()不同, JQuery.val()

$(document).ready(function() {
    if ($("input").value()) {
      $("h1").hide();
    }
});

You should use keyup to know when a key is added/removed from the textbox 您应该使用keyup知道何时在textbox添加/删除密钥。

$(document).ready(function() {
    $("#input_data").keyup(function() {
        var dInput = $(this).val();
        alert(dInput);

    });
});

DEMO 演示

NOTE : Since input can be of any number and checkboxes, radio buttons and button types all are included within the HTML input tag, You should use either name or id or class names as **selectors** . 注意 :由于输入可以是任意数量的,并且复选框,单选按钮和按钮类型都包含在HTML input标签中,因此您应该使用nameidclass名称作为**selectors**

input[type=text]

or, to restrict to text inputs inside forms 或者,仅限于表格内的文本输入

form input[type=text]

or, to restrict further to a certain form, assuming it has id myForm 或者,进一步限制为某种形式,假设它具有id myForm

#myForm input[type=text]

If You want a multiple attribute selector 如果您想要一个多属性选择器

$("input[type='checkbox'][name='ProductCode']")
//assuming input type as checkbox and name of that input being ProductCode

You should use val at the place of value and the solution is : 您应该在value的位置使用val ,解决方案是:

$(document).ready(function() {
    if ($("input").val()) {
      $("h1").hide();
    }
});

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

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