使用jQuery检查隐藏字段中的值

时间:2015-10-06 11:18:02

标签: jquery

我有一个值作为html文本,它位于jquery中的变量中:

var data = "<table width='100%'><tr><td class='messageclass'><strong>Sam </strong>hi<td><td style='color:Gray;' width='85px'><small>20120507<small></td></table><input type='hidden' id='hiddenid' value='4,5,6,7'>"

我对我有价值,可能是4,5,6,7,8,9,10 ....其中任何一个

我想检查值字段<input type='hidden' id='hiddenid' value='4,5,6,7'>中的值是否存在。

检查我的小提琴:FIDDLE

5 个答案:

答案 0 :(得分:2)

您可以使用php.js在JavaScript中使用PHP函数。

function in_array(needle, haystack, argStrict) {
  //  discuss at: http://phpjs.org/functions/in_array/
  // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // improved by: vlado houba
  // improved by: Jonas Sciangula Street (Joni2Back)
  //    input by: Billy
  // bugfixed by: Brett Zamir (http://brett-zamir.me)
  //   example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
  //   returns 1: true
  //   example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
  //   returns 2: false
  //   example 3: in_array(1, ['1', '2', '3']);
  //   example 3: in_array(1, ['1', '2', '3'], false);
  //   returns 3: true
  //   returns 3: true
  //   example 4: in_array(1, ['1', '2', '3'], true);
  //   returns 4: false

  var key = '',
    strict = !! argStrict;

  //we prevent the double check (strict && arr[key] === ndl) || (!strict && arr[key] == ndl)
  //in just one for, in order to improve the performance 
  //deciding wich type of comparation will do before walk array
  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle) {
        return true;
      }
    }
  } else {
    for (key in haystack) {
      if (haystack[key] == needle) {
        return true;
      }
    }
  }

  return false;
}

使用in_array,如果您通过.split()值获得所有值,我们就可以使用。

答案 1 :(得分:2)

var data = "<table width='100%'><tr><td class='messageclass'><strong>Sam </strong>hi<td><td style='color:Gray;' width='85px'><small>20120507<small></td></table><input type='hidden' id='hiddenid' value='4,5,6,7'>";

var myVar = "5",
    hiddenIds = $('<div/>').html(data).find('#hiddenid').val().split(',');

if ($.inArray(myVar, hiddenIds) !== -1) {
    console.log('myVar is in array');
}

Demo

答案 2 :(得分:0)

我认为您想将javascript变量转换为dom节点以访问其中的元素。尝试使用以下js来获取隐藏字段中的值。

 var HTMLSTRING = "<table width='100%'><tr><td class='messageclass'><strong>Sam </strong>hi<td><td style='color:Gray;' width='85px'><small>20120507<small></td></table><input type='hidden' id='hiddenid' value='4,5,6,7'>";


$.each($.parseHTML(HTMLSTRING), function( index, value ) {
    if(value.id=="hiddenid")
        alert(value.value);
});

答案 3 :(得分:0)

&#13;
&#13;
    var data = "<table width='100%'><tr><td class='messageclass'><strong>Sam</strong>hi<td><td style='color:Gray;' width='85px'><small>20120507<small></td></table><input type='hidden' id='hiddenid' value='4,5,6,7'>";
    
    var $elem = $('<div>').html(data)
    alert($elem.find('#hiddenid').val());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

修改

在得到你想要的东西之后,这是解决方案:

因为元素不在DOM中,所以你必须使用RegEx。

var data = "<table width='100%'><tr><td class='messageclass'><strong>Sam</strong>hi<td><td style='color:Gray;' width='85px'><small>20120507<small></td></table><input type='hidden' id='hiddenid' value='4,5,6,7'>";

var hidden_value = data.match(/<input type='hidden' id='hiddenid' value='(.*)'/);
alert(hidden_value[1]);

JSFiddle:https://jsfiddle.net/a4pc8fn6/2/

旧内容,在Fiddle existet之前

我知道您想要获取隐藏输入字段的value

这很容易完成:

Javascript(香草)

var hidden_value = document.getElementById('hiddenid').value;
console.log(hidden_value);

<强>的jQuery

var hidden_value = $('#hiddenid').val();
console.log(hidden_value);

请确保您实际执行data - 变量的输出(在尝试读取值之前也执行此操作),否则输入元素将不会存在于DOM中。