jquery val()不工作 [英] jquery val() not working

查看:92
本文介绍了jquery val()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery val()没有用,这是简单的脚本:

jQuery val() didnt working, this is the simple script:

$("#com_form").submit(function () {
    var name = $("#nama").val();
    var komentar = $("#komentar").val();
    alert.("Hi, " + name + " this is your comment: " + komentar)
});

这是HTML表格:

<form method="post" name="com_form" id="com_form">
  <p>What is your name:<br>

    <input type="text" name="nama" id="nama">
  </p>
  <p>Leave your comment here:<br>

    <input type="text" name="komentar" id="komentar">
  </p>
  <p>
    <input type="submit" name="button2" id="button2" value="Submit">
  </p>
</form>

实际上,我试图创建ajax帖子,值nama被提交但不是komentar 。所以我尝试使用alert进行调试(如上所述)并且仍然komentar没有变化。我该怎么办?

actually, I was tried to create ajax post, the value "nama" is submited but not "komentar". So I tried to debug using alert (like one above) and still "komentar" is not change. What should I do?

推荐答案

你的html中有一些错误。表单应该有action属性,js也不行。

You have some errors in your html. Form should have "action" attribute, also js is not well.

我做了示例html和js文件,它们正常工作。 Html文件通过了w3.org验证,js通过了jslint。

I made example html and js files which works correctly. Html file passed w3.org validation and js passed jslint.



<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("jquery", "1.4.2");
    </script>
    <script type="text/javascript" src="2988992.js"></script>
    <title>Insert title here</title>
</head>
<body>
    <form method="post" name="com_form" id="com_form" action="2988992.html">
    <p>What is your name:<br>
        <input type="text" name="nama" id="nama">
    </p>
    <p>Leave your comment here:<br>
        <input type="text" name="komentar" id="komentar">
    </p>
    <p>
        <input type="submit" name="button2" id="button2" value="Submit">
    </p>
</form>
</body>
</html>





"use strict";
/*global $, alert*/
$(function () {
  $("#com_form").submit(function () {
    var name = $("#nama").val();
    var komentar = $("#komentar").val();
    alert("Hi, " + name + " this is your comment: " + komentar);
    return false; //prevent standard behavior
  });
});

这篇关于jquery val()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆