必须有一个简单的方法来做到这一点,但我已经尝试了我能想到的一切(这可能不会说太多)。我有一个模式,在表单的下拉列表中选择“添加新”选项时会弹出。第一次弹出占位符出现,这就是我想要的。但在那之后,最后输入的值出现而不是占位符。我怎样才能让占位符每次都出现?
这是jQuery:
<script type="text/javascript">
var Classofentry = '';
$('#add-new-text').val() === ''; // Set input text field to blank
console.log($('#add-new-text').val()); // <--------------- This is filled
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
// Get the class
var Classofentry = $(this).attr("class");
//console.log(Classofentry);
//$('#add-new-submit').val() == ''; // Set input text field to blank
//console.log($('#add-new-submit').val()); // <------------- Empty after first change
//$('#add-new-text').val() === ''; // Set input text field to blank
console.log($('#add-new-text').val()); // <--------------- This is filled
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
//console.log(value);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
//dataType: "html",
dataType: "json",
error: errorHandler,
success: success
});
function success(data)
{
if (data[1])
{
// Add new entry
$('#'+Classofentry).append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>");
//alert(data[1]);
}
else
{
// Select the nonunique value by emptying it and appending
$('#'+Classofentry).empty("<option value=''selected=\"selected\">" + data[0] + "</option>").append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>");
//alert(data[0]);
}
}
function errorHandler()
{
//alert('Error with AJAX!');
alert(data[0]);
}
$('#add-new-submit').unbind('click'); // This fixes the problem for multiple entries
$('#add-new').modal('toggle');
});
//$('#add-new-submit').unbind('click');
//$('#upload_form option[value="addnew"]').unbind('click');
});
</script>
和模态:
<!-- add-new field -->
<div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">√ó</button>
<h3 id="add-new-fieldLabel">Add New Field</h3>
</div>
<div class="modal-body">
<p>Would you like to add a new item?</p>
<input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
</div>
</div><!-- /add-new field -->
如您所见,我尝试将 add-new-text 标记简单地设置为空白,但这是行不通的。有什么我可以在 html 中做的吗?