-1

I'm having trouble with jQuery lately.

For instance I'd like to select a text Input and change its value :

var myInput = jQuery('#myInput');
myInput[0].value = "something"; // this works* but text remins grey, and clears as soon as the input gets focus
myInput[0].val("something"); // I believe this is what I should use, however it pops an error :error ²
myVar = $("#something"); // Returns Null (actually, even if I see the function in the debugger that's supposed to return the elements)

*the selector actually is something like : "#aDiv #anOther #anOther #aTable[aDynamicID] tFoot tr[name=[aDymanicName]]"

it works but it's complicated, so I just wrote #something ;)

error ² : Uncaught TypeError: Object # has no method 'val'

Any one understands jQuery enough to see what's going on?? :(

I'm trying to insert filters into a DataTable programatically, here's my <head> :

<head>
    <link rel="stylesheet" type="text/css" href="parminou_style.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="jscript/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript" src="jscript/jquery-migrate-1.2.1.min.js"></script> 
    <script type="text/javascript" src="jscript/prototype.js" ></script>
    <script type="text/javascript" src="jscript/scriptaculous.js" ></script>
    <script src="jscript/tiny_mce/tiny_mce.js" type="text/javascript"></script>

    <script type="text/javascript" src="parminou_js.js" ></script>

    <link href="css/style.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="jscript/jquery.dataTables.js"></script>
    <script type="text/javascript" src="jscript/jeditable.js"></script>
    <script type="text/javascript" src="jscript/jquery-ui-1.8.22.custom.min.js">    </script> 
    <script type="text/javascript" src="jscript/jquery.ui.datepicker.js"></script> 

    <!-- src="jquery-1.10.1.min.js" -->
    <link href="css/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css" /> 
    <link href="css/jquery.dataTabl

sry, it kinda looks like a mess. :/

4

3 回答 3

1

在数据表中插入过滤器的示例

var oTable = $('#example').dataTable(); oTable.fnFilter('测试字符串');

有关如何在数据表中添加过滤器的更多详细信息,请参见以下位置 https://datatables.net/api

也可以使用 jquery 使用 CSS 选择器选择适当的元素,例如可以选择为,

jQuery(".test") 或 jQuery("#input1")

如果所选组件是文本输入,则 val() 肯定会在该组件上工作

于 2013-07-11T18:58:51.570 回答
1

当然myVar = $("something"); 会返回null。你有一个名为 <something> 的元素吗?

您忘记了 #,它表示一个 ID。利用 。来表示类。使用不带上述前缀的选项来选择标签名称。

我刚刚看到你的编辑(这使得这个答案不再适用)。通过发布一些相关的 HTML 来帮助我们。还要确保 $ 定义正确并且不受您拥有的其他脚本的影响——他们的文档可能会告诉您。

于 2013-07-11T18:52:16.740 回答
0

myInput不是一个数组,它是一个类似数组的对象。在它上面使用[0]会返回 jQuery 集合中的第一个 dom 节点。这就是为什么.value有效而.val无效的原因。DOM 节点没有.val方法,而 jQuery 集合有。

var myInput = jQuery('#myInput');
myInput[0].value = "something"; // this works* but text remains grey, and clears as soon as the input gets focus
myInput.val("something"); // this works* but text remains grey, and clears as soon as the input gets focus
myVar = $("#something"); // is prototype

就它保持灰色而言,您还没有做任何使它不变成灰色的事情。只要它一集中注意力就会改变,其他东西正在这样做,而不是这段代码。

于 2013-07-11T19:20:01.610 回答