English 中文(简体)
<option option 选中= “ 选中的” > 文本 </ option> 返回总是一个值, 尽管没有值
原标题:<option selected="selected"> text </option> returns always a value despite having no value

My problem is option value which is
<option selected="selected" disabled="disabled">Select a city...</option>
The problem is : despite this option having no value, php code sees its value as * Select a city *. So, my code if(isset($_POST[ city ]) returns always true even a city is not choosed. How can I solve this?

这里是 < strong> > > html 代码 :

<form id="city_form">

  <select id="city" name="city">
    <option selected="selected" disabled="disabled">Select a city...</option>
    <option value=1 name="city1">City 1</option>
    <option value=2 name="city2">City 2</option>
    <option value=3 name="city3">City 3</option>
    <option value=4 name="city4">City 4</option>
  </select>

  <input type="submit" value="Submit form" id="submit_button" />

</form>
<div id="show_city"></div>

这里是 < 坚固 > jquery < /坚固 > 代码 :

$( #submit_button ).click(function(e) {
  e.preventDefault();
  var data = $( #city_form ).serialize();
  $.post(
         "fetch_city.php",
          data,
          function (response) {
             $( #show_city ).html(response);
          }
        );
});

这里是 < 坚固 > etch_ city.php :

if (isset($_POST[ city ]) && !empty($_POST[ city ])) {

  $city_id = $_POST[ city ]);
  echo $city_id;

} else {
  echo "You did not select a city...!";
}
最佳回答

您无法。 给它一个有意义的监控 < code> value , 并忽略该值出现时的控件 。

<option selected="selected" disabled="disabled" value="">Select a city...</option>
问题回答

我认为这里的答案是jquery 忽略第一个选项上的残疾人。 请尝试从此处删除选中的=“ 选中的” 和“ 残疾” 。 您现在可以看到第一个空白的条目。 当提交表格时, 您的子集会返回假的 。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签