如何使用jQuery查找输入值中的数字并隐藏数字

时间:2018-01-17 12:01:14

标签: javascript jquery

我有这样的输入:

<input type="submit" name="starts" value=" 1154 Update "  class="btn-success">

我需要找到Value中的任何数字并隐藏这样的数字:

<input type="submit" name="starts" value=" Update "  class="btn-success"> 

感谢您的帮助......

4 个答案:

答案 0 :(得分:1)

  

我需要在Value中查找任何数字并隐藏此数字

您可以使用setAttribute

var button = document.querySelector('input[type="submit"][name="starts"]');
var value = button.getAttribute( "value" );
button.setAttribute( "value", value.replace(/\d+/g, "") ); //use trim if required

<强>演示

var button = document.querySelector('input[type="submit"][name="starts"]');
var value = button.getAttribute("value");
button.setAttribute("value", value.replace(/\d+/g, "")); //use trim if required
<input type="submit" name="starts" value=" 1154 Update " class="btn-success">

答案 1 :(得分:0)

你可以做这样的事情来取代这些数字 我已应用KeyupChange个事件。

&#13;
&#13;
import Foundation
import CoreBluetooth





enum BluetoothManagerResponse {
    case success(result:String)
    case fail(error:String)
    case cancelled
}



enum SendEncryption {
    case encrypted(publicKey:String)
    case notEncrypted
}


enum DeviceType {
    case newDevice
    case connectedDevices(device:BluetoothManagerConnectedDevice)
}

typealias BluetoothManagerComlpetion = ((_ response:BluetoothManagerResponse,_ connectedDevice:BluetoothManagerConnectedDevice)->Void)?
typealias BluetoothManagerConnectedDevice = (peripheral:CBPeripheral,characteristic:CBCharacteristic)?
class BluetoothManager:NSObject {


    var manager: CBCentralManager?
    var peripherals = [CBPeripheral]()
    var connectedDevice:BluetoothManagerConnectedDevice = nil
    static let shared = BluetoothManager()

    fileprivate var stx:String {
        return  String(Character.init(UnicodeScalar(0002)))
    }


    fileprivate var etx:String {
        return  String(Character.init(UnicodeScalar(0003)))
    }



    var minimumDistance:Int?
    var currentSegment = 0
    var segments:[Data]?  {
        didSet {
            currentSegment = 0
        }
    }



    var completion:BluetoothManagerComlpetion = nil


    private func encrypt(data:String , withPublicKey key:String) -> String? {
        ...

    }


    var rssiUpdate:((_ rssi:Int) -> Void)?

    func startGettingRSSIUpdate(callback:@escaping (_ rssi:Int) -> Void) {

        self.rssiUpdate = callback
    }



    func send(data:String?,to deviceType:DeviceType,withEncryption type:SendEncryption = .notEncrypted,andMinimumDistance distance:Int? = nil, and completion:BluetoothManagerComlpetion) {
        self.minimumDistance = distance
        hasCanceled = false
        let selector = #selector(deviceNotFound(sender:))
        self.notFoundTimer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: selector, userInfo: nil, repeats: false)
        self.completion = completion



        if let data = data {
            switch type {
            case .encrypted(let publicKey):
                let encrypted = self.encrypt(data: data, withPublicKey: publicKey)
                self.segments = encrypted?.hexadecimalString?.components(withLength: 20).flatMap({$0.hexadecimalData}) ?? []
            case .notEncrypted:
                self.segments = data.hexadecimalString?.components(withLength: 20).flatMap({$0.hexadecimalData}) ?? []
            }
        }
        else {
             self.segments = data?.hexadecimalString?.components(withLength: 20).flatMap({$0.hexadecimalData}) ?? []
        }


        switch deviceType {
        case .newDevice:
             connectedDevice = nil
             manager = CBCentralManager(delegate: self, queue: nil)
        case .connectedDevices(let device):


            guard let device = device else {
                completeWithResponse(response: .fail(error: Messages.Bluetooth.deviceDisconnected))
                return
            }
            switch  device.peripheral.state {
            case .connected:
                device.peripheral.setNotifyValue(true, for: device.characteristic)

            default:
                completeWithResponse(response: .fail(error: Messages.Bluetooth.deviceDisconnected))
            }
        }

    }

    var hasCanceled:Bool = false


    func disconnect() {
        completion = nil
        notFoundTimer?.invalidate()
        notFoundTimer = nil
        manager?.stopScan()
        guard let connectedDevice = self.connectedDevice else { return }
        manager?.cancelPeripheralConnection(connectedDevice.peripheral)

        connectedDevice.peripheral.setNotifyValue(false, for: connectedDevice.characteristic)
        self.connectedDevice = nil

    }

    func cancel() {
        hasCanceled = true
        completeWithResponse(response: .cancelled)
        completion = nil
    }


    func completeWithResponse(response:BluetoothManagerResponse) {
        notFoundTimer?.invalidate()
        notFoundTimer = nil
        manager?.stopScan()
        self.completion?(response,connectedDevice)


    }



    var notFoundTimer:Timer?


    func deviceNotFound(sender:Timer) {
        completeWithResponse(response: .fail(error: Messages.Bluetooth.notFound))

    }


    var rssiValues = [Int]()
    var resetRssiValues = true


    var receivedData:String? {
        didSet {


            guard let receivedData = receivedData,receivedData.contains("\u{03}") else {
                return
            }

            let responseString = String(receivedData.dropLast().dropFirst())
            self.completeWithResponse(response: .success(result: responseString))
        }
    }


    let easedProximity = EasedValue()

    fileprivate func convertRSSItoProximity(rssi:Int) -> Int? {
        let absRssi = Swift.abs(rssi)
        easedProximity.setValue(value: CGFloat(absRssi))
        easedProximity.update()
        if let result = easedProximity.value {
            return Int(result * -1.0)
        }
        return nil
    }

}


//MARK: CBCentralManagerDelegate
extension BluetoothManager: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch  central.state {
        case .poweredOn:
            central.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey:true])

        case .poweredOff:
            self.completeWithResponse(response:.fail(error: Messages.Bluetooth.poweredOff))
        case .resetting:
            self.completeWithResponse(response:.fail(error: Messages.Bluetooth.resetting))
        case .unauthorized:
            self.completeWithResponse(response:.fail(error: Messages.Bluetooth.unauthorized))
        case .unknown:
            self.completeWithResponse(response:.fail(error: Messages.Bluetooth.unknown))
        case .unsupported:
            self.completeWithResponse(response:.fail(error: Messages.Bluetooth.unsupported))
        }
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

        peripheral.delegate = self
        peripheral.discoverServices(nil)


    }

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {

        guard let error = error else {
            self.completeWithResponse(response:.fail(error:  Messages.Bluetooth.unknown))
            return
        }

        self.completeWithResponse(response:.fail(error: error.localizedDescription))

    }



    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {


        func connect(peripheral: CBPeripheral) {

            print("Should connect")
            peripherals.append(peripheral)
            central.connect(peripheral, options: nil)
            central.stopScan()
            easedProximity.reset()
        }


        guard  let name = peripheral.name , name.isMod10 else {return}

        print(name)
        if let distance = self.minimumDistance {


            let rssi = (RSSI as? Int) ?? 0
            let value = self.convertRSSItoProximity(rssi: rssi)

            if let value = value {
                self.rssiUpdate?(value)
            }


            guard let _value = value,_value >= distance else{ return }
        }


         connect(peripheral: peripheral)


    }




}


//MARK: CBPeripheralDelegate
extension BluetoothManager:CBPeripheralDelegate {
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {

        guard let error = error else {
            guard let service = peripheral.services?.first else {
                self.completeWithResponse(response:.fail(error: Messages.Bluetooth.noService))
                return
            }
            peripheral.discoverCharacteristics(nil, for: service)
            return
        }

        self.completeWithResponse(response:.fail(error: error.localizedDescription))

    }


    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
        print("OS Disconect : \(String(describing: error?.localizedDescription))")
    }


    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

        guard let error = error else {
            guard let characteristic = service.characteristics?.first else {
                self.completeWithResponse(response:.fail(error: Messages.Bluetooth.noCharacteristic))
                return

            }
            connectedDevice = (peripheral:peripheral,characteristic:characteristic)
            peripheral.setNotifyValue(true, for: characteristic)
            return
        }

        self.completeWithResponse(response:.fail(error: error.localizedDescription))

    }


    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {

        guard characteristic.isNotifying  else {
            manager?.cancelPeripheralConnection(peripheral)
            return

        }
        guard let error = error else {
            guard let segments = segments , segments.count > 0 else {
                 return
            }


            let selector = #selector(send(sender:))
            Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: selector, userInfo: (peripheral:peripheral,characteristic:characteristic), repeats: true)

            return
        }

        self.completeWithResponse(response:.fail(error: error.localizedDescription))
    }





    internal func send(sender:Timer) {

        if let userInfo = sender.userInfo as? (peripheral:CBPeripheral,characteristic:CBCharacteristic),
           let segments = segments,
           currentSegment >= 0 && currentSegment < segments.count && segments.count > 0
        {

            userInfo.peripheral.writeValue(segments[currentSegment], for: userInfo.characteristic, type: .withoutResponse)
        }
        else {
            sender.invalidate()
        }


        currentSegment = currentSegment + 1

    }



    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        //guard characteristic.isNotifying  else {return}
        //peripheral.setNotifyValue(false, for:characteristic)
        guard let error = error else {
            guard let receivedData = characteristic.value?.toString else {
                self.completeWithResponse(response:.fail(error: Messages.Bluetooth.invalidReadData))
                return
            }



            if receivedData.first == "\u{02}"  {
                self.receivedData = nil
            }

            self.receivedData = (self.receivedData ?? "")  +  receivedData
            return
        }


        self.completeWithResponse(response:.fail(error: error.localizedDescription))


    }


}
&#13;
$("#inputRegex").change(function()
{
  $(this).val($(this).val().replace(/\d+/g, ''))
});

$("#inputRegex").keyup(function()
{
  $(this).val($(this).val().replace(/\d+/g, ''))
});
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

如果你想点击提交按钮隐藏这些数字:

$(":submit").click(function() {
  string value =  $(this).val().toString();
  value = $.trim(value.replace(/\d+/g, ""));
  $(this).val(value);

});

如果你想在加载时隐藏:

$( document ).ready(function() {
$(":submit").each(function() {
      string value =  $(this).val().toString();
      value = $.trim(value.replace(/\d+/g, ""));
      $(this).val(value);

        });
})

这将删除所有输入类型提交的值中的所有数字。 这是fiddle

答案 3 :(得分:-1)

HTML部分:

<input type="submit" name="starts" value=" 1154 Update " class="btn-success">
<input type="submit" name="starts" value=" 1155 Update " class="btn-success">
<input type="submit" name="starts" value=" 1156 Update " class="btn-success">

和jQuery Part:

$('input[type="submit"]').val($('input[type="submit"]').val().replace(/\d+/g, "").trim()); 

fiddle可能会对您有所帮助。

如果你想隐藏它。您可以将任何数据属性设置为提交按钮

<input type="submit" name="starts" data-id ="1154" value="Update " class="btn-success">