0

我的html中有两个文本框(goalText和goalText1)和一个按钮(goalreach)。我的目标:当我在1个文本框(goalText)中输入数值时,它应该被转换为json并被存储。因此,即使在我运行应用程序 5 天后,它也应该被存储。现在,当我在其他文本框(goalText1)中输入数值并且它匹配时,我只是显示消息匹配。这是演示,我正在尝试让我知道值可以存储在 json 中,并且可以在必要时检索。我编写的代码如下:

$("#goalreach").click(function () {
  var contact = new Object();
  contact.goalDist = "$("#goalText.value ").val()";
  var jsonText = JSON.stringify(contact);
  if (jsonText == ($("#goalText1.value").val())) {
      document.getElementById('divid').innerHTML = 'Match';
  }
});

我知道,我也犯了很多简单的括号和“错误,但我是新手,如果你能帮助我。

4

3 回答 3

0

试试下面的代码:

$("#goalreach").click(function () {
    var contact = new Object();
    contact.goalDist = $("#goalText").val();
    var jsonText = JSON.stringify(contact);
    if (jsonText == ($("#goalText1").val())) {
        document.getElementById('divid').innerHTML = 'Match';
    }
});

或者

$("#goalreach").click(function () {
    var goalText = $("#goalText").val();
    var goalText1 = $("#goalText1").val();
    if (goalText == goalText1) {
        document.getElementById('divid').innerHTML = 'Match';
    }
});
于 2013-07-22T14:03:12.797 回答
0

尝试这个

$("#goalreach").click(function () {

var contact = new Object();

var goalDist  = '$("#goalText.value").val()';
var jsonText = JSON.stringify(contact.goalDist);

if(jsonText==($("#goalText1.value").val()))
{
     document.getElementById('divid').innerHTML = 'Match';
}
});
于 2013-07-22T14:04:22.657 回答
0

首先,您必须比较 2 个对象或 2 个字符串,并且在 中goalDist,您应该存储值(顺便说一句,您得到 jQuery 对象,而且$("#goalText")somejQueryObject.val()通常等于document.getElementById("goalText").value)...

这可以这样做:

$("#goalreach").click(function () {
  // Create an object with the single property "goalDist"
  var contact = { goalDist : $("#goalText").val() };

  // Makes it be a string (it will in this simple example : `"{"goalDist":<the value of goalTest>}"`
  var jsonText = JSON.stringify(contact);

  // Creates a string from an equivalent object bound on the second field
  var jsonText2 = JSON.stringify({ goalDist : $("#goalText2").val() });

  // Compares the 2 strings
  if (jsonText === jsonText2) {
      document.getElementById('divid').innerHTML = 'Match';
  }
});
于 2013-07-22T14:06:09.643 回答