如何使一个领域有一个前缀值和X删除值

问题描述:

我希望我的形式有一个字段使用前缀值是这样的:如何使一个领域有一个前缀值和X删除值

纽约X

让用户可以通过点击X来改变它。换句话说,他们点击X,他们得到一个文本字段来写任何他们想要的东西。

有没有简单的方法来做到这一点,或者它是很多代码?

-

BOX9:把你的代码后不输入提交时预填充值:

<form class="questionform" name="questionform-0" id="questionform-0"> 

<textarea class="question-box" cols="12" rows="5" id="question-box-' . $questionformid . '" name="title" type="text" maxlength="200" size="28"></textarea> 

<span class="prefilled"><script language="javascript"> document.write('' + geoip_city() +''); </script><a href="#"> X</a></span> 

<input type="text" name="question" style="display:none"> 

<input type="button" name="ask" value="Publicar" onclick="askquestion('questionform-0'); window.location.reload(true);"> 

</form> 

<script type="text/javascript"> 
$(".prefilled a").click(function() { 
    $(this).closest(".prefilled").hide().next().show().focus(); 
    return false; // Prevent page from jumping to the top 
}); 
</script> 
+0

这是一个更多的javascript问题。不涉及PHP。我已经适当地重新签了字。 – dqhendricks 2011-01-11 00:55:31

+0

这种形式机制在大多数框架中称为“暗示”。 – 2011-01-11 00:58:39

一个通用的解决方案,只要标记是如下:

HTML:

<span class="prefilled">New York <a href="#">X</a></span> 
<input type="text" name="city" style="display:none" value="New York"> 

的Javascript:

$(".prefilled a").click(function() { 
    $(this).closest(".prefilled").hide().next().val("").show().focus(); 
    return false; // Prevent page from jumping to the top 
}); 

看到你所得到的值你可能想要设置输入的值,如下所示:

$("input[name=question]").val(geoip_city()); 

如果你使用jQuery这是很简单的事:

<form> 
    <span id="city">New York</span> 
    <a id="edit" href="#"> X </a> 
</form> 

和脚本是:

$("#edit").click(function() { 
    $("#city").replaceWith('<input id="edit-city" type="text" name="city" />'); 
    $("#edit-city").focus(); 
    return false; 
}); 

See it in action