文本输入到句子案例

时间:2015-08-26 10:28:04

标签: javascript html

我有一个表格,保存到数据库并生成PDF。

我希望将输入文本转换为句子大小写。尝试style="text-transform:capitalize"但它仍然保存输入,因为它输入到数据库中。我怎么能这样做,也许用JavaScript?

<form class="form-style-9" name="litterregistration" id="LitterReg" method="post">

<li>
                        <input type="text" name="AKennel" required class="field-style field-split align-left" placeholder="Kennel Name" style="text-transform:capitalize" />
                        <input type="text" name="BDamMother" required class="field-style field-split align-right" placeholder="Dame (Mother)" style="text-transform:capitalize"/>
</li>

  <input type="submit" value="Save as PDF" onClick="Onsubmit1();"  />
  <input type="button" onclick="ClearFormFields()" value="Clear All Fields">
</form

<script>
function Onsubmit1()
{
   document.litterregistration.action = "tcpdf/examples/form-litter-regis.php"

    return true;
}
</script>

2 个答案:

答案 0 :(得分:0)

请注意,这是一个非scalabe解决方案,只适用于此特定代码

嗨,先说几句: - 你调用ClearFormFields()函数,但它不是在任何地方构建的; - 函数的名称onSubmit1()并不是很好。 - 您错过了表单标记中的/>

考虑到这一点,这应该是(THIS)技巧:

使用Javascript:

<script type="text/javascript">
function mySubmitAction(myAction) {
    with (document.getElementById("LitterReg")) {
        AKennel.value = AKennel.value.toUpperCase();
        BDamMother.value = BDamMother.value.toUpperCase();
    }

    document.litterregistration.action = myAction;

    return true;
}
</script>

<强> HTML

<form class="form-style-9" name="litterregistration" id="LitterReg" method="post">
<li>
    <input type="text" name="AKennel" required class="field-style field-split align-left" placeholder="Kennel Name" style="text-transform:capitalize" />
    <input type="text" name="BDamMother" required class="field-style field-split align-right" placeholder="Dame (Mother)" style="text-transform:capitalize"/>
</li>

  <input type="submit" value="Save as PDF" onclick="mySubmitAction('tcpdf/examples/form-litter-regis.php');" />
  <input type="button" onclick="ClearFormFields()" value="Clear all fields" />
</form>

答案 1 :(得分:0)

&#13;
&#13;
var $input1 = $( '#input-1' );

$.toSentenceCase = function ( value ) {
  var val = value.split( ' ' );
  
  for ( var i = 0, l = val.length; i < l; i++ ) {
    val[i] = val[i].charAt(0).toUpperCase() + val[i].substr(1);
  }
  
  return val.join( ' ' );
}

$input1.on( 'blur', function ( e ) {
  var $this = $( this );
  
  $this.val( $.toSentenceCase( $this.val() ) );
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" id="input-1">
</form>
&#13;
&#13;
&#13;