无法获取文本框值

时间:2016-10-22 18:38:32

标签: javascript jquery html

嗨我试图用一些输入做某事但输入(除了市政)显示为空字符串。所以即使id填充文本框,建筑也会显示为空。没有控制台错误。



$('#editAddress').submit(function (e) {
		e.preventDefault();
		var mun = $("#municipalities option:selected").text();
		var city = $("#city").val();
		var street  = $("#street").val();
		var building = $("#building").val();
		var floor = $("#floor").val();
		var addr = $("#address").val();
		alert("bldg: "+building);
		editAddr( mun,city,street,building,floor,addr);
	});

   <form id="editAddress" action = '' method = 'post'>
                            <select id ="municipalities" class ="form_select" placeholder="Municipality">
                                <option>Select Municipality</option>
                                <option>Beirut</option>
                                <option>Mlikh</option>
                                <option>Haret Hreik</option>
                                <option>Haret Saida</option>
                                <option>Jounieh</option>
                                <option>Baalbeck</option>
                                <option>Hermel</option>
                                <option>Tebnine</option>
                                <option>Jwaya</option>
                            </select>
                            <input type="text"     name="city"         id="city"           class="form_input" placeholder="City"/>
                            <input type="text"     name="street"       id="street"         class="form_input" placeholder="Street"/>
                            <input type="text"     name="building"     id="building"       class="form_input" placeholder="Building"/>
                            <input type="text"     name="floor"        id="floor"          class="form_input" placeholder="Floor"/>
                            <textarea              name="address"      id="address"        class="form_input" placeholder="Address Notes"></textarea>
                            <input type="submit" name="register" class="form_submit" id="editAddr" value="Edit Address" />
                        </form>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

jQuery已经处理过:

var mun = $("#municipalities").val()

其他一切都是有序的:

    $(function() {
        $('#editAddress').submit(function (e) {
            e.preventDefault();
            var mun = $("#municipalities option:selected").text();
            var city = $("#city").val();
            var street  = $("#street").val();
            var building = $("#building").val();
            var floor = $("#floor").val();
            var addr = $("#address").val();
           console.log(mun, city, street, building, floor, addr);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="editAddress" action = '' method = 'post'>
    <select id ="municipalities" class ="form_select" placeholder="Municipality">
        <option>Select Municipality</option>
        <option>Beirut</option>
        <option>Mlikh</option>
        <option>Haret Hreik</option>
        <option>Haret Saida</option>
        <option>Jounieh</option>
        <option>Baalbeck</option>
        <option>Hermel</option>
        <option>Tebnine</option>
        <option>Jwaya</option>
    </select>
    <input type="text"     name="city"         id="city"           class="form_input" placeholder="City"/>
    <input type="text"     name="street"       id="street"         class="form_input" placeholder="Street"/>
    <input type="text"     name="building"     id="building"       class="form_input" placeholder="Building"/>
    <input type="text"     name="floor"        id="floor"          class="form_input" placeholder="Floor"/>
    <textarea              name="address"      id="address"        class="form_input" placeholder="Address Notes"></textarea>
    <input type="submit" name="register" class="form_submit" id="editAddr" value="Edit Address" />
</form>

答案 1 :(得分:0)

Hmmmm。似乎为我工作。确保包含jQuery库,如下面的代码示例所示。

继续在建筑物字段中输入内容,然后使用该数据发出警报。

我还没注意到其他错误吗?如果是,请对该信息发表评论。

&#13;
&#13;
$('#editAddress').submit(function (e) {
		e.preventDefault();
		var mun = $("#municipalities option:selected").text();
		var city = $("#city").val();
		var street  = $("#street").val();
		var building = $("#building").val();
		var floor = $("#floor").val();
		var addr = $("#address").val();
		alert("bldg: "+building);
		editAddr( mun,city,street,building,floor,addr);
	});
function editAddr(){
  return false;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="editAddress" action = '' method = 'post'>
                            <select id ="municipalities" class ="form_select" placeholder="Municipality">
                                <option>Select Municipality</option>
                                <option>Beirut</option>
                                <option>Mlikh</option>
                                <option>Haret Hreik</option>
                                <option>Haret Saida</option>
                                <option>Jounieh</option>
                                <option>Baalbeck</option>
                                <option>Hermel</option>
                                <option>Tebnine</option>
                                <option>Jwaya</option>
                            </select>
                            <input type="text"     name="city"         id="city"           class="form_input" placeholder="City"/>
                            <input type="text"     name="street"       id="street"         class="form_input" placeholder="Street"/>
                            <input type="text"     name="building"     id="building"       class="form_input" placeholder="Building"/>
                            <input type="text"     name="floor"        id="floor"          class="form_input" placeholder="Floor"/>
                            <textarea              name="address"      id="address"        class="form_input" placeholder="Address Notes"></textarea>
                            <input type="submit" name="register" class="form_submit" id="editAddr" value="Edit Address" />
                        </form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

尝试以下步骤

1)尝试删除e.preventDefault();

来运行代码

2)从输入框中删除表单输入类并添加表单控件

<input type="text"  name="city" id="city"  class="form-control" placeholder="City"/>
<input type="text"  name="street" id="street" class="form-control" placeholder="Street"/>
<input type="text"  name="building" id="building" class="form-control" placeholder="Building"/>
<input type="text"  name="floor"  id="floor"  class="form-control" placeholder="Floor"/>
<textarea  name="address"  id="address"  class="form-control" placeholder="Address Notes"></textarea>
<input type="submit" name="register" class="form_submit" id="editAddr" value="Edit Address" />

答案 3 :(得分:-1)

document.getElementById('address').value