Javascript 如何创建文本验证框
我试图弄清楚如何创建一个文本验证框,该框假定在单击按钮时出现,并且在用户必须输入某个值之后出现,如果该值正确,则按钮应继续执行其正常功能 这是我的代码:Javascript 如何创建文本验证框,javascript,php,jquery,html,button,Javascript,Php,Jquery,Html,Button,我试图弄清楚如何创建一个文本验证框,该框假定在单击按钮时出现,并且在用户必须输入某个值之后出现,如果该值正确,则按钮应继续执行其正常功能 这是我的代码: <div id="texto-cupon" class="panel-boton-canjear"> <?php if ($comprobarCanjear == 1 || $comprobarCanjear == 2 || $comprobarCanjear == 3) { ?> <inpu
<div id="texto-cupon" class="panel-boton-canjear">
<?php
if ($comprobarCanjear == 1 || $comprobarCanjear == 2 || $comprobarCanjear == 3) {
?>
<input disabled=disabled id="canjear" class="btn" type="button" value="<?php echo JText::_('COM_CUPHONEO_BOTON_CANJEAR'); ?>" data-id="<?php echo $valor->item_id; ?>" data-codigo="<?php echo $value->value; ?>" />
<?php
} elseif($comprobarCanjear == 4) {
?>
<input id="canjear" class="btn btn-info" type="button" value="<?php echo JText::_('COM_CUPHONEO_BOTON_CANJEAR'); ?>" data-id="<?php echo $valor->item_id; ?>" data-codigo="<?php echo $value->value; ?>" />
<?php
}
?>
</div>
如果您不担心样式,您可以使用浏览器内置的“promt()”函数。否则,您可以创建自己的窗口。第一个选择很简单。做下面这样的事情
var input = prompt("Title");
if( input != null && input == "expectedValue")
{
window.location.href = "index.php?option=com_cuphoneo&view=miscuponess&layout=default"
}
else
{
//invalid input
}
试试这个,但一定要把一些东西换成你自己的。对话框需要jQuery UI 示例HTML
<button id="canjear">Click me</button>
<div class="dialog" style="display:none;">
<input type="text" id="prompt" placeholder="Insert value hello" />
<button id="test">Submit</button>
</div>
我想你需要这样的东西: 编辑:在页面上输入 HTML
也许这更接近你想要的。你能告诉我,你面临的问题是什么吗?我只注意到ajax请求可能不起作用。在成功函数中,打开一个带有输入字段和验证函数的弹出窗口,然后在验证后转到window.location.href…@NishanSenevirathna ajax请求可以正常工作。我什么都没注意到,只是不知道该怎么做:(@webkit你能举个例子吗?我不太擅长jQuery atmsure,我会在下面的答案中给你写jQuery..你会做html/css..还要为我澄清一下,用户应该在文本字段中输入什么值?有没有办法只使用按钮我必须验证字段而不是新按钮?你想要相同的按钮您单击,打开一个弹出窗口,然后使用相同的按钮在弹出窗口中提交表单?您真的需要弹出窗口吗?或者您的意思是让此输入字段显示在原始按钮上方?我希望的方式就是当用户单击按钮时,应该出现一个文本字段,一旦用户输入了正确的值,它就应该继续在ajax中运行。
<button id="canjear">Click me</button>
<div class="dialog" style="display:none;">
<input type="text" id="prompt" placeholder="Insert value hello" />
<button id="test">Submit</button>
</div>
jQuery(function () {
jQuery("#canjear").click(function (e) {
jQuery('.dialog').dialog({
title: 'Prompt',
modal: true,
open: function (event, ui) {
jQuery('#test').click(function (evt) {
var theValue = jQuery('#prompt').val();
if (theValue == "hello") {
jQuery.ajax({
url: "index.php?option=com_cuphoneo&task=miscuponess.canjearCupon",
type: "POST",
data: {
id: jQuery(e.currentTarget).data("id"),
codigo: jQuery(e.currentTarget).data("codigo")
}
success: function (response) {
window.location.href = "index.php?option=com_cuphoneo&view=miscuponess&layout=default"
}
});
}
});
}
});
});
});
?>
<input type="text" id="txt" class="hidden">
<input id="canjear" class="btn btn-info" type="button"...
$("#canjear").on('click', btnClick);
function btnClick() {
$("#txt").show();
$(this).off().on('click', function() {
jQuery.ajax({
url: "index.php?option=com_cuphoneo&task=miscuponess.canjearCupon",
type: "POST",
data: {id:jQuery(this).data("id"), codigo:jQuery(this).data("codigo")},
success: function (data) {
// check against codigo ** codigo is returned by the server right??
if ($("#txt").val() != data.codigo) {
// invalid ** write code for invalid input.
alert("Please enter valid whatever into the field.");
return false;
} else {
// valid input
window.location.href = "index.php?option=com_cuphoneo&view=miscuponess&layout=default";
}
});
}
}