我需要名字和姓氏的前两个字母,它将在像 Salman Shaikh 这样的文本框中输入,所以它会在我的页面中的某个位置作为 SASH 我需要这个。
5 回答
Here is live example with jQuery.
I'll paste the code in here also -
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
$("body").append(firstName+surname);
});
});
In case you don't know - jQuery is JS library. You can get it here . Download the latest jquery release and insert it in your html/php file between the tags, just like you normally insert js scripts.
Attention: You must insert my created code only after you have inserted the jQuery library, otherwise it won't work.
EDIT: Updated to your suggested version -
Code -
jQuery(document).ready(function() {
jQuery("#form1").change(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
if(firstName != "" && surname != "") {
$("body").append(firstName+surname);
}
});
});
EDIT 2: Final example per your request -
And code here -
HTML
<form id="form1">
<input type="text" name="name" id="name" placeholder="name" />
<br />
<input type="text" name="surname" id="surname" placeholder="surname" />
<br />
<h2 id="text">WSCPP<span id="nameSurname"></span><span id="time"></span><span id="randomNr"></span></h2>
</form>
jQuery/JS -
jQuery(document).ready(function() {
jQuery("#form1").change(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
if(firstName != "" && surname != "") {
$("#nameSurname").text(firstName+surname);
}
});
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate(); var year = currentTime.getFullYear();
$("#time").text(day + "" + month + "" +year);
$("#randomNr").text(Math.floor(Math.random()*90000+9999));
});
Check following for program
How about this:
$_REQUEST['firstname'] = 'Salman ';
$_REQUEST['lastname'] = 'Shaikh';
$firstname = strtoupper(substr($_GET['firstname'], 0, 2));
$lastname = strtoupper(substr($_GET['lastname'], 0, 2));
echo $firstname.$lastname;
我假设您正在将数据发布到某个页面,这就是您php
的问题中有标签的原因。
$firstname = substr($_POST['firstname'],2);
$lastname = substr($_POST['lastname'],2);
Explode the string to an array.
$name_array=explode(' ',$name);
$first=substr(trim($name_array[0]),0,2);
$last=substr(trim($name_array[1]),0,2);